package com.xinxing.city.recovery.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.xinxing.city.recovery.BR;
import com.xinxing.city.recovery.R;
import com.xinxing.city.recovery.entitys.Catalogs;
import com.xinxing.city.recovery.interfaces.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class BaseDBAdapter<T> extends RecyclerView.Adapter<BaseDBAdapter.ViewHolder> {
    private List<T> mDatas = new ArrayList<>();
    @LayoutRes
    protected int mItemLayoutRes;
    private OnItemClickListener mClickListener;
    protected int variableId;
    private ViewDataBinding binding2;
    public BaseDBAdapter(@LayoutRes int itemLayoutRes,int variableId){
        this.mItemLayoutRes = itemLayoutRes;
        this.variableId = variableId;
    }

    @NonNull
    @Override
    public BaseDBAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), mItemLayoutRes, parent, false);
        BaseDBAdapter.ViewHolder viewHolder = new BaseDBAdapter.ViewHolder(binding.getRoot(),mClickListener);
        viewHolder.setBinding(binding);
        binding2 = binding;
        return viewHolder;
    }
    public void bindItem(ViewDataBinding binding,T item,int p){}
    @Override
    public void onBindViewHolder(@NonNull BaseDBAdapter.ViewHolder holder, int position) {
        holder.binding.setVariable(variableId,mDatas.get(position));
        holder.binding.executePendingBindings();

        holder.bindItem1(holder.getBinding(),mDatas.get(position),position );
    }

    public void setmDatas(List<T> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }
    public List<T> getmDatas(){
        return mDatas;
    }
    @Override
    public int getItemCount() {
        return null == mDatas ? 0 : mDatas.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ViewDataBinding binding;
        private OnItemClickListener mListener;
        public  void bindItem1(ViewDataBinding binding,T item,int p){
            bindItem(binding,item,p);
        }
        public ViewHolder(@NonNull View itemView,OnItemClickListener mListener) {
            super(itemView);
            this.mListener =mListener;
        }
        public ViewDataBinding getBinding() {
            return binding;
        }

        public void setBinding(ViewDataBinding binding) {
            this.binding = binding;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if(mListener!=null) {
                mListener.onItemClick(view, getAdapterPosition());
            }
        }
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }
}
